package com.jxdinfo.hussar.example.common.schedulingtask.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxdinfo.hussar.example.common.schedulingtask.dao.ProWorkorderMapper;
import com.jxdinfo.hussar.example.common.schedulingtask.model.ProWorkorder;
import com.jxdinfo.hussar.example.common.schedulingtask.service.ProWorkorderService;
import com.jxdinfo.hussar.example.common.schedulingtask.vo.ProWorkorderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 生产工单表 服务实现类
 * </p>
 *
 * @author 乔镔
 * @since 2024-04-08
 */
@Service("com.jxdinfo.hussar.example.common.schedulingtask.service.impl.ProWorkorderServiceImpl")
public class ProWorkorderServiceImpl extends ServiceImpl<ProWorkorderMapper, ProWorkorder> implements ProWorkorderService {

    @Autowired
    private ProWorkorderMapper proWorkorderMapper;

    @Autowired
    private ProWorkorderService proWorkorderService;

    @Override
    public void manualSynchronization() {
        LocalDateTime nowTime = LocalDateTime.now();
        // 1查询erp所有的生产工单信息
        List<ProWorkorderVO> allOrder = proWorkorderMapper.selectAllOrder();
        // 2生产工单信息转换成主键-对象map集合
        Map<Long, ProWorkorderVO> allOrderMap = allOrder.stream().collect(Collectors.toMap(ProWorkorderVO::getWorkorderId, t -> t));
        // 3生产工单信息中的id转换成id列表
        List<Long> erpAllOrderIdList = allOrder.stream().map(ProWorkorderVO::getWorkorderId).collect(Collectors.toList());
        // 4查询所有的mes生产工单信息
        QueryWrapper<ProWorkorder> proWorkorderQueryWrapper = new QueryWrapper<>();
        proWorkorderQueryWrapper.eq("deleted_flag","0");
        List<ProWorkorder> mesProWorkorders = proWorkorderMapper.selectList(proWorkorderQueryWrapper);
        // 5mes中的工单信息转换成id列表,
        List<Long> mesIdList = mesProWorkorders.stream().map(ProWorkorder::getErpProId).collect(Collectors.toList());
        // 6找出erp中有但是mes中没有的id，这写id需要插入
        ArrayList<Long> erpHasMesNoIdList = new ArrayList<>();
        for (int i = 0; i < erpAllOrderIdList.size(); i++) {
            if (!mesIdList.contains(erpAllOrderIdList.get(i))) {
                erpHasMesNoIdList.add(erpAllOrderIdList.get(i));
            }
        }
        if (erpHasMesNoIdList.size() > 0) {
            // 循环创建对象，保存待要插入的结果
            ArrayList<ProWorkorder> insertList = new ArrayList<>();
            for (Long insertId : erpHasMesNoIdList) {
                ProWorkorderVO proWorkorderVO = allOrderMap.get(insertId);
                ProWorkorder proWorkorder = new ProWorkorder();
                proWorkorder.setWorkorderId(proWorkorderVO.getWorkorderId());  // 主键
                proWorkorder.setWorkorderCode(proWorkorderVO.getWorkorderCode());  // 工单编码
                proWorkorder.setWorkshopId(proWorkorderVO.getWorkshopId());  // 车间ID
                proWorkorder.setWorkshopCode(proWorkorderVO.getWorkshopCode());  // 车间编码
                proWorkorder.setWorkshopName(proWorkorderVO.getWorkshopName());  // 车间名称
                proWorkorder.setOrderSource(proWorkorderVO.getOrderSource());  // 来源类型
                proWorkorder.setSourceCode(proWorkorderVO.getSourceCode());  // 来源单据
                proWorkorder.setProductId(proWorkorderVO.getProductId());  // 产品id
                proWorkorder.setQuantity(proWorkorderVO.getQuantity());  // 生产数量
                proWorkorder.setStatus(proWorkorderVO.getStatus());  // 单据状态
                proWorkorder.setStartDate(proWorkorderVO.getStartDate());  // 计划开始日期
                proWorkorder.setRequestDate(proWorkorderVO.getRequestDate());  // 计划完成日期
                proWorkorder.setWarehouseId(proWorkorderVO.getWarehouseId());  // 仓库主键
                proWorkorder.setProductCode(proWorkorderVO.getProductCode());  // 产品编码
                proWorkorder.setProductName(proWorkorderVO.getProductName());  // 产品名称
                proWorkorder.setProductSpc(proWorkorderVO.getProductSpc());  // 规格型号
                proWorkorder.setMeasureId(proWorkorderVO.getMeasureId());  // 单位id
                proWorkorder.setUnitOfMeasure(proWorkorderVO.getUnitOfMeasure());  // 单位名称
                proWorkorder.setWarehouseCode(proWorkorderVO.getWarehouseCode());  // 仓库编码
                proWorkorder.setWarehouseName(proWorkorderVO.getWarehouseName());  // 仓库名称
                proWorkorder.setEnableFlag(proWorkorderVO.getEnableFlag());  // 生效状态
                proWorkorder.setCreateBy(proWorkorderVO.getCreateBy());
                proWorkorder.setCreateTime(proWorkorderVO.getCreateTime());
                proWorkorder.setUpdateBy(proWorkorderVO.getUpdateBy());
                proWorkorder.setUpdateTime(proWorkorderVO.getUpdateTime());
                proWorkorder.setErpProId(proWorkorderVO.getWorkorderId()); // erp主键
                proWorkorder.setScheduleTime(nowTime);
                proWorkorder.setWorkorderName(
                        new StringBuilder()
                                .append(proWorkorderVO.getProductName())
                                .append("【")
                                .append(proWorkorderVO.getQuantity().toString())
                                .append("】")
                                .append(proWorkorderVO.getUnitOfMeasure()).toString());// 工单名称
                insertList.add(proWorkorder);
                if (proWorkorder.getProductId() == null){
                    insertList.remove(proWorkorder);
                }
            }
            proWorkorderService.saveBatch(insertList);
        }
        // 7找出erp和mes中都有的，这些需要更新
        ArrayList<Long> updateIdList = new ArrayList<>();
        for (int i = 0; i < mesIdList.size(); i++) {
            if (erpAllOrderIdList.contains(mesIdList.get(i))){
                updateIdList.add(mesIdList.get(i));
            }
        }
        if (updateIdList.size()>0) {
            ArrayList<ProWorkorder> proWorkorderUpdateList = new ArrayList<>();
            List<ProWorkorder> updateProWorkorderList = proWorkorderMapper.selectBatchIds(updateIdList);
            for (ProWorkorder proWorkorder : updateProWorkorderList) {
                Long erpProId = proWorkorder.getErpProId();
                ProWorkorderVO proWorkorderVO = allOrderMap.get(erpProId);
                proWorkorder.setWorkorderCode(proWorkorderVO.getWorkorderCode());  // 工单编码
                proWorkorder.setWorkshopId(proWorkorderVO.getWorkshopId());  // 车间ID
                proWorkorder.setWorkshopCode(proWorkorderVO.getWorkshopCode());  // 车间编码
                proWorkorder.setWorkshopName(proWorkorderVO.getWorkshopName());  // 车间名称
                proWorkorder.setOrderSource(proWorkorderVO.getOrderSource());  // 来源类型
                proWorkorder.setSourceCode(proWorkorderVO.getSourceCode());  // 来源单据
                proWorkorder.setProductId(proWorkorderVO.getProductId());  // 产品id
                proWorkorder.setQuantity(proWorkorderVO.getQuantity());  // 生产数量
                proWorkorder.setStatus(proWorkorderVO.getStatus());  // 单据状态
                proWorkorder.setStartDate(proWorkorderVO.getStartDate());  // 计划开始日期
                proWorkorder.setRequestDate(proWorkorderVO.getRequestDate());  // 计划完成日期
                proWorkorder.setWarehouseId(proWorkorderVO.getWarehouseId());  // 仓库主键
                proWorkorder.setProductCode(proWorkorderVO.getProductCode());  // 产品编码
                proWorkorder.setProductName(proWorkorderVO.getProductName());  // 产品名称
                proWorkorder.setProductSpc(proWorkorderVO.getProductSpc());  // 规格型号
                proWorkorder.setMeasureId(proWorkorderVO.getMeasureId());  // 单位id
                proWorkorder.setUnitOfMeasure(proWorkorderVO.getUnitOfMeasure());  // 单位名称
                proWorkorder.setWarehouseCode(proWorkorderVO.getWarehouseCode());  // 仓库编码
                proWorkorder.setWarehouseName(proWorkorderVO.getWarehouseName());  // 仓库名称
                proWorkorder.setEnableFlag(proWorkorderVO.getEnableFlag());  // 生效状态
                proWorkorder.setCreateBy(proWorkorderVO.getCreateBy());
                proWorkorder.setCreateTime(proWorkorderVO.getCreateTime());
                proWorkorder.setUpdateBy(proWorkorderVO.getUpdateBy());
                proWorkorder.setUpdateTime(proWorkorderVO.getUpdateTime());
                proWorkorder.setErpProId(proWorkorderVO.getWorkorderId());
                proWorkorder.setScheduleTime(nowTime);
                proWorkorder.setWorkorderName(
                        new StringBuilder()
                                .append(proWorkorderVO.getProductName())
                                .append("【")
                                .append(proWorkorderVO.getQuantity().toString())
                                .append("】")
                                .append(proWorkorderVO.getUnitOfMeasure()).toString());// 工单名称
                proWorkorderUpdateList.add(proWorkorder);
            }
            proWorkorderService.updateBatchById(proWorkorderUpdateList);
        }
        // 8找出erp中没有，mes中有的，这些需要删除
        ArrayList<Long> erpNoMesHasIdList = new ArrayList<>();
        for (int i = 0; i < mesIdList.size(); i++) {
            if (!erpAllOrderIdList.contains(mesIdList.get(i))){
                erpNoMesHasIdList.add(mesIdList.get(i));
            }
        }
        if (erpNoMesHasIdList.size()>0) {
            proWorkorderMapper.updateIsDeleteById(erpNoMesHasIdList);
        }
    }
}
